<template>
	<view class="goods-detail-container">
		<!-- 轮播图 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="3000" :circular="true">
			<swiper-item v-for="(item, i) in goodsDetail.pics" :key="i">
				<image :src="item.pics_big" @click="preview(i)"></image>
			</swiper-item>
		</swiper>
		<!-- 商品信息区域 -->
		<view class="goodsMeg" v-if="goodsDetail.goods_name">
			<text class="goodsPrice">￥{{goodsDetail.goods_price.toFixed(2)}}</text>
			<view class="midbox">
				<text class="goodsTitle">{{goodsDetail.goods_name}}</text>
				<view>
					<uni-icons :type="collectType" size="18" color="gray" @click="collect"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<text class="delivery">快递：免运费</text>
		</view>
		<!-- 商品详情区 -->
		<rich-text :nodes="goodsDetail.goods_introduce"></rich-text>
		<!-- 商品导航栏 -->
		<view class="goods_nav">
		  <!-- fill 控制右侧按钮的样式 -->
		  <!-- options 左侧按钮的配置项 -->
		  <!-- buttonGroup 右侧按钮的配置项 -->
		  <!-- click 左侧按钮的点击事件处理函数 -->
		  <!-- buttonClick 右侧按钮的点击事件处理函数 -->
		  <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick" @buttonClick="buttonClick" />
		</view>
	</view>
</template>

<script>
	import { mapState, mapMutations, mapGetters } from 'vuex'
	export default {
		data() {
			return {
				goodsID: 0,
				goodsDetail: {},
				collectType: 'star',
				options: [
					{
						icon: 'headphones',
						text: '客服'
					}, {
						icon: 'shop',
						text: '店铺',
						info: 2,
						infoBackgroundColor:'#007aff',
						infoColor:"red"
					}, {
						icon: 'cart',
						text: '购物车',
						info: 0
					}],
					buttonGroup: [{
					  text: '加入购物车',
					  backgroundColor: '#ff0000',
					  color: '#fff'
					},
					{
					  text: '立即购买',
					  backgroundColor: '#ffa200',
					  color: '#fff'
					}
				]
			};
		},
		computed: {
			...mapState('m_cart', ['cart']),
			...mapGetters('m_cart', ['total'])
		},
		watch: {
			total: {
				handler(newVal) {
					const findResult = this.options.find(x => x.text === '购物车')
					if(findResult) findResult.info = newVal
				},
				immediate: true
			}
		},
		methods: {
			...mapMutations('m_cart', ['addToCart']),
			async getGoodsDetail() {
				const { data: res } = await uni.$http.get('/api/public/v1/goods/detail', { goods_id: this.goodsID })
				if(res.meta.status !== 200) return uni.$showMsg()
				 res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g, '<img style="display:block;" ').replace(/webp/g, 'jpg')
				this.goodsDetail = res.message
			},
			preview(i) {
				uni.previewImage({
					current: i,
					urls: this.goodsDetail.pics.map(x => x.pics_big)
				})
			},
			collect() {
				if(this.collectType === 'star') {
					this.collectType = 'star-filled'
				} else {
					this.collectType = 'star'
				}
				
			},
			onClick (e) {
				uni.showToast({
					title: `点击${e.content.text}`,
					icon: 'none'
				})
			},
			buttonClick (e) {
				
				if(e.content.text === "加入购物车") {
					this.options[2].info++
					// { goods_id, goods_name, goods_price, goods_count, goods_small_logo, goods_state }
					const goods = {
						goods_id: this.goodsDetail.goods_id,
						goods_name: this.goodsDetail.goods_name,
						goods_price: this.goodsDetail.goods_price,
						goods_count: 1,
						goods_small_logo: this.goodsDetail.goods_small_logo,
						goods_state: true
					}
					this.addToCart(goods)
					// uni.switchTab({
					// 	url: "/pages/cart/cart"
					// })
				}
				
			}
		},
		onLoad(options) {
			this.goodsID = options.goods_id
			this.getGoodsDetail()
		}
	}
</script>

<style lang="scss">
.goods-detail-container {
	margin-bottom: 50px;
}
swiper {
	height: 750rpx;
	margin-bottom: 10px;
	image {
		width: 100%;
		height: 100%;
	}
}
.goodsMeg {
	padding: 0 5px;
	margin-bottom: 5px;
	display: flex;
	flex-direction: column;
	justify-content: space-around;
	align-items: flex-start;
	.goodsPrice {
		color: #C00000;
		font-size: 17px;
	}
	.midbox {
		display: flex;
		justify-content: space-between;
		align-items: center;
		.goodsTitle {
			font-size: 15px;
		}
		view {
			display: flex;
			flex-direction: column;
			flex-shrink: 0;
			align-items: center;
			justify-content: center;
			font-size: 15px;
			color: gray;
			border-left: 1px solid #dcdcdc;
			padding-left: 5px;
		}
	}
	.delivery {
		color: gray;
		font-size: 12px;
	}
}
uni-goods-nav {
	position: fixed;
	bottom: 0;
	left: 0;
	z-index: 999;
	width: 100%;
}
</style>
